package com.qianfeng.openapi.web.master.utils;

import com.qianfeng.openapi.web.master.entity.ScheduleJob;
import com.qianfeng.openapi.web.master.exception.QuartzException;
import com.qianfeng.openapi.web.master.quartz.MyQuartzJob;
import org.quartz.*;

/**
 * @Description:定时任务的工具类
 * 创建、修改、删除、恢复、暂停、运行一次...
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/27
 * @Time: 下午4:12
 */
public class QuartzUtils {


    /**
     * 创建定时任务
     * @param scheduleJob
     * @param scheduler
     */
    public  static  void   createTask(ScheduleJob scheduleJob, Scheduler scheduler){
        try {
            //1,创建JobDetail
            JobDetail jobDetail = JobBuilder.newJob(MyQuartzJob.class)
                    .withIdentity(SysConstant.JOB_KEY+scheduleJob.getJobId())
                    .build();

            //目的：为了不同的定时任务执行不同的业务
            //scheduleJob  beanName  methodName
            jobDetail.getJobDataMap().put(SysConstant.TASK_DATA,scheduleJob);

            //2,创建Trigger
            CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                    .withSchedule(CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()))
                    .withIdentity(SysConstant.TRIGGER_KEY+scheduleJob.getJobId()).build();

            //3,注册触发器和任务
            scheduler.scheduleJob(jobDetail,cronTrigger);

            //4,开启任务
            scheduler.start();

        } catch (SchedulerException e) {
            throw  new QuartzException("创建定时任务失败！");
           // throw  new SchedulerException("创建定时任务失败！");
        }
    }

    /**
     * 修改定时任务
     * 只能修改定时任务的cron表达式
     */
    public  static void  updateScheduler(ScheduleJob scheduleJob,Scheduler scheduler){

        TriggerKey triggerKey = TriggerKey.triggerKey(SysConstant.TRIGGER_KEY+scheduleJob.getJobId());
        //1,得到原来的触发器
        try {
           CronTrigger cronTrigger =  (CronTrigger) scheduler.getTrigger(triggerKey);
            //2,重新构建新的触发器
            cronTrigger = cronTrigger.getTriggerBuilder().
                    withSchedule
                            (CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()))
                    .withIdentity(triggerKey).build();
            //3,告知调度器  调度器重置新的触发器
            scheduler.rescheduleJob(triggerKey,cronTrigger);

        } catch (SchedulerException e) {
            throw  new QuartzException("修改定时任务异常！");
        }

    }

    public static  void  deleteScheduler(long jobId,Scheduler scheduler){
        try {
            // scheduler.deleteJobs(List<JobKey> var1)//批量删除
            scheduler.deleteJob(JobKey.jobKey(SysConstant.JOB_KEY+jobId));
        } catch (SchedulerException e) {

            throw  new QuartzException("删除定时任务异常");
        }
    }

    /**
     * 暂停任务
     * @param jobId
     * @param scheduler
     */
    public static  void  pauseScheduler(long jobId,Scheduler scheduler){
        try {
            scheduler.pauseJob(JobKey.jobKey(SysConstant.JOB_KEY+jobId));
        } catch (SchedulerException e) {

            throw  new QuartzException("删除定时任务异常");
        }
    }
    /**
     * 恢复任务
     * @param jobId
     * @param scheduler
     */
    public static  void  resumeScheduler(long jobId,Scheduler scheduler){
        try {
            scheduler.resumeJob(JobKey.jobKey(SysConstant.JOB_KEY+jobId));
        } catch (SchedulerException e) {

            throw  new QuartzException("删除定时任务异常");
        }
    }

    /**
     * 运行任务
     * @param jobId
     * @param scheduler
     */
    public static  void  runOnceTask(long jobId,Scheduler scheduler){
        try {
            scheduler.triggerJob(JobKey.jobKey(SysConstant.JOB_KEY+jobId));
        } catch (SchedulerException e) {

            throw  new QuartzException("删除定时任务异常");
        }
    }





}
